﻿Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging
Imports Subgurim.Controles
Imports System.Web.Configuration

Partial Class WebForm_CommonControl_GoogleMapExif
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        '設定Key,如果設定錯誤,則會出現金鑰已使用
        '取金鑰網址：http://code.google.com/intl/zh-TW/apis/maps/signup.html

        Select Case Me.Request.Url.Host.ToLower()
            Case "123.204.250.225"
                GMap1.Key = "ABQIAAAAFe-hZUdRjfRyOpItR081jhSRXRJMf1qDdjW_gwBDV6Hce0aifBQJn1tDzrjnUzdaronDcv80AmziuA"
            Case "pc01"
                GMap1.Key = "ABQIAAAAFe-hZUdRjfRyOpItR081jhRruZ6ecNvkd2BMFxz35EQBxvC-OxRlJn7aPUKbwt0dFrrVV37sSoiSQQ"
            Case "carpool.tpc.gov.tw"
                GMap1.Key = "ABQIAAAAFe-hZUdRjfRyOpItR081jhRlNsshoFXJdzd_S8S2VN86ChRxrBTyJ9mjreU_5ISGiyeOIk1B8BJ1oQ"
            Case "61.60.124.102"
                GMap1.Key = "ABQIAAAA4CFy8Y50TiAqG6EFkoz_oRQDCpNHzi4V5B_lroOKcmJjvc7DdRTGQjTtnB0GIpksCn_fBwECa0qGKw"
            Case "61.60.124.162"
                GMap1.Key = "ABQIAAAAFe-hZUdRjfRyOpItR081jhQQWF_f7ufRZSIOd0gVpiPBY_zQchTSysWnd150J2-JKZNU4B-xNAJjTQ"
            Case "carpool.ntpc.gov.tw"
                GMap1.Key = "ABQIAAAA4CFy8Y50TiAqG6EFkoz_oRSGPD8mNufb_lbm0FX4NrUzGopjNxQWXUdodze0MrQweTMtjU82nDRHYw"
            Case "pc10"
                GMap1.Key = "ABQIAAAAgWC7gnuIm6QAu6JZL5qrexTUWk_G-JQ-diF3yurLfvepZZspLxTTjpTFrN9tj9NVY2AR-FNGBNclhQ"
            Case "svr09"
                GMap1.Key = "ABQIAAAA4CFy8Y50TiAqG6EFkoz_oRSi-vq0KCug8e9hYBs7U27XGzSH8xSRnVC2MPHTiTOtVbpWfBM7_L71YA"
            Case Else
                GMap1.Key = WebConfigurationManager.AppSettings("GoogleMapKey")
        End Select
        GMap1.Visible = False
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Public Sub BindData()
        If Me.GMap1 Is Nothing Then
            Exit Sub
        End If

        Dim filePath As String = Server.MapPath(Me.PicFilePath)
        If File.Exists(filePath) Then
            GMap1.reset()
            Try
                Dim theImage As Image = New Bitmap(filePath)
                Dim propItems As PropertyItem() = theImage.PropertyItems
                Dim count As Integer = 0
                Dim propItem As PropertyItem
                Dim NorthPos, EastPos As String

                Dim chrGPSLatitudeRef As Char = "N" '暫訂緯度為N(北緯) 
                Dim chrGPSLongitudeRef As Char = "E" '暫訂經度為E(東經)

                For Each propItem In propItems
                    Select Case propItem.Id.ToString("x")
                        Case "1"
                            chrGPSLatitudeRef = BitConverter.ToChar(propItem.Value, 0)
                        Case "2"
                            '北緯latitude
                            '===================================
                            'propItem.Value(0) = 2
                            'propItem.Value(8) = 4
                            'propItem.Value(16) = 6
                            'theImage.SetPropertyItem(propItem)
                            '===================================

                            'degrees(將byte[0]~byte[3]轉成uint, 除以byte[4]~byte[7]轉成的uint)
                            Dim d As Double = BitConverter.ToUInt32(propItem.Value, 0) * 1 / BitConverter.ToUInt32(propItem.Value, 4)
                            'minutes(將byte[8]~byte[11]轉成uint, 除以byte[12]~byte[15]轉成的uint)
                            Dim m As Double = BitConverter.ToUInt32(propItem.Value, 8) * 1 / BitConverter.ToUInt32(propItem.Value, 12)
                            'seconds(將byte[16]~byte[19]轉成uint, 除以byte[20]~byte[23]轉成的uint)
                            Dim s As Double = BitConverter.ToUInt32(propItem.Value, 16) * 1 / BitConverter.ToUInt32(propItem.Value, 20)
                            '計算緯度的數值, 如果是南緯, 要乘上(-1)
                            If chrGPSLatitudeRef.Equals("N"c) Then
                                NorthPos = ((s / 60 + m) / 60) + d
                            Else
                                NorthPos = (((s / 60 + m) / 60) + d) * -1
                            End If

                            '呈現緯度的表示式：例如，37 deg 44' 28.64" N 
                            Me.strLatitude = String.Format("{0:#} deg {1:#}' {2:#.00}"" {3}", d, m, s, chrGPSLatitudeRef)
                        Case "3"
                            chrGPSLongitudeRef = BitConverter.ToChar(propItem.Value, 0)
                        Case "4"
                            '東經longitude
                            '===================================
                            'propItem.Value(0) = 1
                            'propItem.Value(8) = 3
                            'propItem.Value(16) = 5
                            'theImage.SetPropertyItem(propItem)
                            '===================================

                            'degrees(將byte[0]~byte[3]轉成uint, 除以byte[4]~byte[7]轉成的uint)
                            Dim d As Double = BitConverter.ToUInt32(propItem.Value, 0) * 1 / BitConverter.ToUInt32(propItem.Value, 4)
                            'minutes(將byte[8]~byte[11]轉成uint, 除以byte[12]~byte[15]轉成的uint)
                            Dim m As Double = BitConverter.ToUInt32(propItem.Value, 8) * 1 / BitConverter.ToUInt32(propItem.Value, 12)
                            'seconds(將byte[16]~byte[19]轉成uint, 除以byte[20]~byte[23]轉成的uint)
                            Dim s As Double = BitConverter.ToUInt32(propItem.Value, 16) * 1 / BitConverter.ToUInt32(propItem.Value, 20)
                            '計算緯度的數值, 如果是西經, 要乘上(-1)
                            If chrGPSLongitudeRef.Equals("E"c) Then
                                EastPos = ((s / 60 + m) / 60) + d
                            Else
                                EastPos = (((s / 60 + m) / 60) + d) * -1
                            End If

                            '呈現經度的表示式：例如，97 deg 15' 56.72" W
                            Me.strLongitude = String.Format("{0:#} deg {1:#}' {2:#.00}"" {3}", d, m, s, chrGPSLongitudeRef)
                    End Select
                Next propItem
                'theImage.Save(filePath & "fixed.jpg")

                If NorthPos <> "" And EastPos <> "" Then
                    Me.SetGmap(NorthPos, EastPos)
                    Me.GMap1.Visible = True
                Else
                    Me.GMap1.Visible = False
                End If
            Catch ex As Exception
                Me.GMap1.Visible = False
            End Try
        Else
            Me.GMap1.Visible = False
        End If

    End Sub

    Public Sub SetGmap(ByVal NorthPos As Double, ByVal EastPos As Double)
        '參考來源
        'http://en.googlemaps.subgurim.net/ejemplos/empezar.aspx

        '增加畫面上的控制項
        GMap1.addControl(New GControl(GControl.preBuilt.MapTypeControl)) '顯示地圖類型(地圖、衛星、混合地圖)
        'GMap1.addControl(New GControl(GControl.preBuilt.SmallMapControl)) '小地圖控制項(上下移、放大縮小)
        GMap1.addControl(New GControl(GControl.preBuilt.LargeMapControl)) '大地圖控制項(上下移、放大縮小)
        'GMap1.addControl(New GControl(GControl.extraBuilt.MarkCenter))'中心點標十字
        'GMap1.addControl(New GControl(GControl.extraBuilt.TextualCoordinatesControl)) '顯示座標
        'GMap1.addControl(New GControl(GControl.preBuilt.GOverviewMapControl)) '右下方顯示預覽地圖

        '設定位置
        Dim latlong As New GLatLng(NorthPos, EastPos)

        '置中、調顯示比例
        GMap1.setCenter(latlong, 13)

        '增加文字說明
        Dim marker As New GMarker(latlong)
        Dim window As New GInfoWindow(marker, "<center><b>" & Me.strLatitude & "<br/>" & Me.strLongitude & "</b></center>", False)
        GMap1.addInfoWindow(window)

        '允許滾輪縮放
        GMap1.enableHookMouseWheelToZoom = True
    End Sub

    Public Property PicFilePath() As String
        Get
            Return ViewState("PicFilePath")
        End Get
        Set(ByVal value As String)
            ViewState("PicFilePath") = value
            Me.BindData()
        End Set
    End Property

    Public Property strLatitude() As String
        Get
            Return ViewState("strLatitude")
        End Get
        Set(ByVal value As String)
            ViewState("strLatitude") = value
        End Set
    End Property

    Public Property strLongitude() As String
        Get
            Return ViewState("strLongitude")
        End Get
        Set(ByVal value As String)
            ViewState("strLongitude") = value
        End Set
    End Property

    Public Property Width() As Integer
        Get
            Return ViewState("Width")
        End Get
        Set(ByVal value As Integer)
            ViewState("Width") = value
            Me.GMap1.Width = value
        End Set
    End Property

    Public Property Height() As Integer
        Get
            Return ViewState("Height")
        End Get
        Set(ByVal value As Integer)
            ViewState("Height") = value
            Me.GMap1.Height = value
        End Set
    End Property
End Class
